Decode String
Question
Given an encoded string, return it’s decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].
Examples:
|
|
Analysis
- 遇到数字压入栈count中,注意数字可能不止一位
- 遇到字母衔接到结果字符串res后
- 遇到[,将当前的结果字符串res压入栈中保存,并且将res置空,方便后续重复字符串的赋值
- 遇到],弹出count与str栈顶元素,在str栈顶元素后衔接count个当前的res,最后将所得字符串赋值给res
- 直接在结果字符串处理,可以方便只有一个的字符串直接衔接在res后
Code
|
|
Surrounded Regions
Question
Given a 2D board containing 'X'
and 'O'
(the letter O), capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
For example,
|
|
After running your function, the board should be:
|
|
Analysis
找到位于最外四条边的O,由其出发进行BFS,将该过程中所有遇到的O均改变为#,最后遍历整个方阵。将其中的#改为O,其余的都变为X
- 用visited实现的方法很蠢,没有意义- -
- BFS中需要控制边界的条件,首先判断i/j的加一或减一操作后是否会有越界,再分四种状况进行BFS
Code
|
|